#include "SolidColorBackground.h"

using namespace OBR;

SolidColorBackground::SolidColorBackground(Spectrum _color)
    : radiance(_color), Background("shaders/Background/SolidColorBackground.cu")
{
    this->program_desc.miss.entryFunctionName = "__miss__solid_color_background";
    this->program_desc.kind = OPTIX_PROGRAM_GROUP_KIND_MISS;
    this->program_desc.flags = OPTIX_GEOMETRY_FLAG_NONE;
}

SolidColorBackground::~SolidColorBackground()
{
}

void SolidColorBackground::uploadSbtData()
{
    SolidColorBackgroundData data;
    data.radiance = this->radiance;

    this->sbt_data.resize(sizeof(data));
    this->sbt_data.upload(&data, sizeof(data));
}

void SolidColorBackground::_buildFromConfig(const Json::Value &config)
{
    if (config["type"].asString().compare("solid color") != 0)
    {
        std::cerr << "wrong config value is sent to "
                     "SolidColorBackground::_buildFromConfig"
                  << std::endl;
        std::cerr << "got:" << config << std::endl;
        throw std::runtime_error("invalid parameter");
    }
    ConfigHelpers::loadSpectrum(config["color"], &(this->radiance));
}

OptixProgramGroupOptions &SolidColorBackground::getProgramGroupOptions()
{
    return this->program_opt;
}

OptixProgramGroupDesc &SolidColorBackground::getProgramGroupDesc()
{
    this->program_desc.miss.module = this->cuda_module->optix_module;
    return this->program_desc;
}